package net.oschina.j2cache.util;

import net.oschina.j2cache.CacheException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 标准的 Java 序列化
 *
 * @author Winter Lau(javayou@gmail.com)
 */
public class JavaSerializer implements Serializer {
  @Override
  public String name() {
    return "java";
  }

  @Override
  public byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream)) {
      oos.writeObject(obj);
      return byteArrayOutputStream.toByteArray();
    }
  }

  @Override
  public Object deserialize(byte[] bits) throws IOException {
    if (bits == null || bits.length == 0) {
      return null;
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(bits);
    try (ObjectInputStream ois = new ObjectInputStream(bais)) {
      return ois.readObject();
    } catch (ClassNotFoundException e) {
      throw new CacheException(e);
    }
  }
}
